package com.fis.test3.controller;

import com.fis.test3.Repository.FPRepository;
import com.fis.test3.model.BindingData;
import com.fis.test3.model.FiberPerpare;
import com.fis.test3.model.FiberTypes;
import com.fis.test3.model.RealTimeProcess;
import com.fis.test3.service.FiberPerpareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@Controller
public class TestController {
    @Autowired
    private FPRepository userRepository;

    @Autowired
    private FiberPerpareService fpService;

    @RequestMapping("/searchUser/{contractId}")
    public @ResponseBody
    List<FiberPerpare> searchUser(@PathVariable("contractId") String contractId) {
        List<FiberPerpare> result = this.userRepository.findBycontractIdContaining(contractId);
        return result;
    }

    //http://localhost:8080/fpsave?contractId=1&cusUnit=1&prodDescrip=1&fiberType=1&jumperType=1&fiberNum=1&fiberLength=1&coreNum=1&remarks=1
    @RequestMapping(value = "/fpsave",method = RequestMethod.GET)
    @ResponseBody
    public FiberPerpare fpsave(@RequestParam(value="contractId") String contractId,
                                         @RequestParam(value="cusUnit") String cusUnit,
                                         @RequestParam(value="prodDescrip") String prodDescrip,
                                         @RequestParam(value="fiberType") String fiberType,
                                         @RequestParam(value="jumperType") String jumperType,
                                         @RequestParam(value="fiberNum") int fiberNum,
                                         @RequestParam(value="fiberLength") int fiberLength,
                                         //时间为实时时间
                                         @RequestParam(value="coreNum") int coreNum,
                                         @RequestParam(value="remarks") String remarks
    ){
        FiberPerpare fp = new FiberPerpare();
        fp.setContractId(contractId);
        fp.setCusUnit(cusUnit);
        fp.setProdDescrip(prodDescrip);
        fp.setFiberType(fiberType);
        fp.setJumperType(jumperType);
        fp.setFiberNum(fiberNum);
        fp.setFiberLength(fiberLength);
        fp.setCoreNum(coreNum);
        fp.setRemarks(remarks);
        fpService.FiberPerpareSave(fp);
        return fp;
    }

//http://localhost:8080/beforewearlooseshow?contractId=12345

    @RequestMapping(value = "/beforewearlooseshow",method = RequestMethod.GET)
    @ResponseBody
    public String beforewearlooseshow(Model model,@RequestParam(value="contractId") String contractId){
       Collection<BindingData> bindingDatas = fpService.getProductNumList(contractId);
        Collection<FiberTypes> fiberTypeses = fpService.getfibTypeNameLength(contractId);
        //产品编号bindingData.productNum
        model.addAttribute("bindingDatas",bindingDatas);
        //产品类型fiberTypeses.fibTypeName+"-"+fiberTypeses.fiberLength+"M"
        model.addAttribute("fiberTypeses",fiberTypeses);
        return "success";
    }

    //http://localhost:8080/beforewearloosesave?productNum=12345-1&fibTypeName=1-1&iCname=A123
    @RequestMapping(value = "/beforewearloosesave",method = RequestMethod.GET)
    @ResponseBody
    public BindingData beforewearloosesave(Model model, @RequestParam(value="productNum") String productNum,
                            @RequestParam(value="fibTypeName") String fibTypeName,
                            @RequestParam(value="iCname") String iCname){
        BindingData bd = new BindingData();
        bd.setProductNum(productNum);
        bd.setFibTypeName(fibTypeName);
        bd.setiCname(iCname);
        fpService.BindingDataSave(bd);
        return bd;
    }

    @RequestMapping(value = "/wearloose",method = RequestMethod.GET)
    @ResponseBody
    public RealTimeProcess wearloose(Model model,@RequestParam(value="iCname") String iCname,
                                     @RequestParam(value="workPosition") String workPosition,
                                     @RequestParam(value="workProcess") String workProcess,
                                     @RequestParam(value="operatorName") String operatorName){
        RealTimeProcess rtP = new RealTimeProcess();
        if ("A"==iCname.substring(0,1)){

        }else {
            rtP.setiCname(iCname);
            rtP.setWorkPosition(workPosition);
            rtP.setWorkProcess(workProcess);
            rtP.setOperatorName(operatorName);

        }
        return rtP;
    }


}
